#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: ix-passwd
# REQUIRE: mountcritlocal
# BEFORE: ix-zfs

. /etc/rc.freenas

generate_master.passwd()
{

	local IFS="|"
	local f="bsdusr_username bsdusr_unixhash bsdusr_uid bsdgrp_gid bsdusr_full_name bsdusr_home bsdusr_shell bsdusr_password_disabled bsdusr_locked"
	eval local $f
	local sf=$(var_to_sf $f)

	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "SELECT
		$sf
	FROM
		account_bsdusers AS u
        INNER JOIN account_bsdgroups AS g
                ON u.bsdusr_group_id=g.id" | \
	while eval read $f; do
		local passwd="${bsdusr_unixhash}"
		if [ "${bsdusr_password_disabled}" -eq 1 ]; then
			passwd="*"
		elif [ "${bsdusr_locked}" -eq 1 ]; then
			passwd="*LOCKED*"
		fi
		echo "${bsdusr_username}:${passwd}:${bsdusr_uid}:${bsdgrp_gid}::0:0:${bsdusr_full_name}:${bsdusr_home}:${bsdusr_shell}"
	done > /etc/master.passwd

	if dirsrv_enabled nis
	then
		echo "${NIS_PWDSTR}" >> /etc/master.passwd
	fi
}

group_membership()
{
	local IFS="|"
	local f="bsdusr_username"
	eval local $f
	local sf=$(var_to_sf $f)
	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "SELECT
		$sf
	FROM
		account_bsdgroupmembership AS m
	CROSS JOIN
		account_bsdusers AS u
		ON m.bsdgrpmember_user_id = u.id
	WHERE
		bsdgrpmember_group_id=$1" | tr \\n ,
}

samba_add_group()
{
	local group="${1}"
	local members="${2}"
	local samba_tool="/usr/local/bin/samba-tool"

	${samba_tool} group list|grep -iw "${group}" >/dev/null 2>&1
	if [ "$?" != "0" ] ; then
		${samba_tool} group add "${group}" >/dev/null 2>&1
	fi
	if [ -n "${members}" ] ; then
		${samba_tool} group addmembers \
			"${group}" "${members}" >/dev/null 2>&1
	fi
}

generate_group()
{
	local IFS="|"
	local f="bsdgrp_group bsdgrp_gid bsdgrp_builtin id"
	eval local $f
	local sf=$(var_to_sf $f)

	LOGNAME="$(whoami)"
	export LOGNAME

	${FREENAS_SQLITE_CMD} ${RO_FREENAS_CONFIG} "SELECT
		$sf
	FROM
		account_bsdgroups" | \
	while eval read $f; do
		echo -n "${bsdgrp_group}:*:${bsdgrp_gid}:"

		local members="$(group_membership ${id} | \
			sed -e 's/,$//g' | tr -d \\n)"
		echo -n "${members}"
		echo
	done > /etc/group

	if dirsrv_enabled nis
	then
		echo "${NIS_GRPSTR}" >> /etc/group
	fi
}

generate_all()
{
	RO_FREENAS_CONFIG=$(ro_sqlite ${name} 2> /tmp/${name}.fail && rm /tmp/${name}.fail)
	trap 'rm -f ${RO_FREENAS_CONFIG}' EXIT
	generate_group
	generate_master.passwd
	pwd_mkdb -p /etc/master.passwd
}

name="ix-passwd"
start_cmd='generate_all'
stop_cmd=':'

load_rc_config $name
run_rc_command "$1"
